//
//  IndentTableViewCell.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/8/11.
//  Copyright (c) 2015年 szb. All rights reserved.
//

#import "childBaseTableViewCell.h"
#import "BPImageTool.h"
#import "indentModel.h"
#import "BPImageTool.h"
#import "MJExtension.h"
#import "orderModel.h"
#import "AccountTool.h"
#import "AFNetworking.h"
#import "BPhttpTool.h"
#import "PayOverSuccessViewController.h"


@implementation childBaseTableViewCell
@synthesize delegate;

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"status";
//    childBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //if (!cell) {
        childBaseTableViewCell *cell = [[childBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    //}
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        CGFloat imageY = 15.0f;
        CGFloat imageW = 140.0 - imageY * 2.0f;
        CGFloat imageX = 10.0f;
        _productImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageW)];
        _productImage.contentMode = UIViewContentModeScaleAspectFit;
        [_productImage.layer setBorderColor:[BPGlobaBGColor CGColor]];
        [_productImage.layer setBorderWidth:0.7];
        [self addSubview:_productImage];
        
        CGFloat labelX = CGRectGetMaxX(_productImage.frame) + 10.0f;
        CGFloat labelW = 200.0f;
        CGFloat labelY = imageY + 10.0f;
        _Name = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelW, 16.0f)];
        _Name.font = [UIFont systemFontOfSize:15.0f];
        [self addSubview:_Name];
        
        _Standard = [[UILabel alloc] initWithFrame:CGRectMake(labelX,labelY + (_Name.height + 7.0f), labelW, 13.0f)];
        self.Standard.alpha = 0.3;
        _Standard.font = [UIFont systemFontOfSize:13.0f];
        [self addSubview:_Standard];
        
        CGFloat timeY = CGRectGetMaxY(_Standard.frame) + 7;
        _Time = [[UILabel alloc] initWithFrame:CGRectMake(labelX, timeY, labelW, 13.0f)];
        self.Time.alpha = 0.5;
        _Time.font = [UIFont systemFontOfSize:13.0f];
        [self addSubview:_Time];
        
        CGFloat btnY = CGRectGetMaxY(_Time.frame) + 7;
//        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        _rightButton.frame = CGRectMake(labelX, btnY + 20, 75.0, 25.0);
//        _rightButton.backgroundColor = BPGlobaThemeColor;
//        [self setupButton:_rightButton];
        
//        CGFloat btnX = CGRectGetMaxX(_rightButton.frame)+10.0f;
//        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _leftBtn.frame = CGRectMake(btnX, btnY + 20, 75.0, 25.0f);
//        _leftBtn.backgroundColor = pinkBtnColor;
//        [self setupButton:_leftBtn];
        
        
        self.xianJia = [[UILabel alloc] initWithFrame:CGRectMake(labelX, btnY + 10, 80, 20)];
        self.xianJia.textColor = [UIColor redColor];
        [self addSubview:self.xianJia];
        
        
        
        
        self.yuanJia = [[UILabel alloc] initWithFrame:CGRectMake(labelX + 100, btnY + 10, 80, 20)];
        self.yuanJia.textColor = [UIColor grayColor];
        self.yuanJia.font = [UIFont systemFontOfSize:13];
        [self addSubview:self.yuanJia];

        
        
        
        self.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView = [[UIImageView alloc] init];
        
        BPLineView *line = [[BPLineView alloc] initWithFrame:CGRectMake(0, 140.0, BPScreenW, 0.7)];
        [self addSubview:line];
    }
    return self;
}

- (void)setupButton:(UIButton *)button
{
    [button.layer setMasksToBounds:YES];
    [button.layer setCornerRadius:10.0f];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 4.0f, 0, 0);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -3.0, 0, 0);
    button.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    [self addSubview:button];

}

- (void)setUpCell:(productModel *)model
{
    // 原价
    NSString *string = @"原价:";
    NSString *Yuan = [NSString stringWithFormat:@"%@%@", string, model.oprice];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13], NSStrikethroughStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]};
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:Yuan attributes:dic];
    self.yuanJia.attributedText = attString;

    // 现价
    NSString *strM = @"¥";
    self.xianJia.text = [NSString stringWithFormat:@"%@%@", strM, model.price];
    
    
    self.Name.text = [NSString stringWithFormat:@"%@-%@",model.title,model.category];
    self.Standard.text = [NSString stringWithFormat:@"规格:%@   %@页",model.page_standard,model.pagetotal];
    if (model.create_time == nil) {
        NSArray *array = [model.makeDate componentsSeparatedByString:@" "];
        model.create_time = array[0];
    }
    
    self.Time.text = model.create_time;
    if ([model.picture isKindOfClass:[NSNull class]]) {
        model.picture = nil;
    }
    
    [BPImageTool setUpUIImageView:self.productImage addNsstringUrl:model.picture];
//    [self setupstateButtonLabel:@"删除" buttonImage:@"deleBtnImage" addbutton:_leftBtn];
//    [self.leftBtn addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
    
//    if ([model.status isEqualToString:@"3"]) {
//        [self setupstateButtonLabel:@"继续编辑" buttonImage:@"continueEditBtnImage" addbutton:_rightButton];
//        [self.rightButton addTarget:self action:@selector(jixubianji) forControlEvents:UIControlEventTouchUpInside];
//    }else if([model.status isEqualToString:@"2"] || [model.status isEqualToString:@"1"]){
//        [self setupstateButtonLabel:@"查看详情" buttonImage:@"detailBtnImage" addbutton:_rightButton];
//        [self.rightButton addTarget:self action:@selector(xiangqing) forControlEvents:UIControlEventTouchUpInside];
//    }else if([model.status isEqualToString:@"0"]){
//        [self setupstateButtonLabel:@"继续编辑" buttonImage:@"continueEditBtnImage" addbutton:_rightButton];
//        [self.rightButton addTarget:self action:@selector(jixubianji) forControlEvents:UIControlEventTouchUpInside];
//    }
}


- (void)setUpCell2:(indentModel *)model
{
    
    // 原价
    NSString *string = @"原价:";
    NSString *Yuan = [NSString stringWithFormat:@"%@%@", string, model.oprice];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13], NSStrikethroughStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]};
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:Yuan attributes:dic];
    self.yuanJia.attributedText = attString;
    
    // 现价
    NSString *strM = @"¥";
    self.xianJia.text = [NSString stringWithFormat:@"%@%@", strM, model.price];
    
    
    self.Standard.text = [NSString stringWithFormat:@"规格:%@   %@页",model.page_standard,model.pagetotal];
    
    NSString *str = [NSString stringWithFormat:@"￥%@",model.total];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:str];
    NSRange contentRange = {1,[content length] -1};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:contentRange];
    self.Name.text = [NSString stringWithFormat:@"%@--%@",model.name,model.category];
    //self.Standard.text = model.orderid;
//    self.Time.attributedText = content;
    
    // self.Time 是 订单号.
    self.Time.text = model.send_time;
    self.Time.font = [UIFont systemFontOfSize:12];
    self.Time.alpha = 0.5;
    self.Time.textColor = [UIColor blackColor];
    if ([model.pic isKindOfClass:[NSNull class]]) {
        model.pic = nil;
    }
    
    [BPImageTool setUpUIImageView:self.productImage addNsstringUrl:model.pic];
    
    if ([model.status isEqualToString:@"100"]) {
        [self setupstateButtonLabel:@"立即支付" buttonImage:@"payBtnImage" addbutton:_leftBtn];
        [self.leftBtn addTarget:self action:@selector(zhifu) forControlEvents:UIControlEventTouchUpInside];
        
        [self setupstateButtonLabel:@"删除" buttonImage:@"deleBtnImage" addbutton:_rightButton];
        [self.rightButton addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
        
    }else if([model.status isEqualToString:@"200"]||[model.status isEqualToString:@"300"]||[model.status isEqualToString:@"400"]){
        [self setupstateButtonLabel:@"查看物流" buttonImage:@"expressBtnImage" addbutton:_rightButton];
        [self.rightButton addTarget:self action:@selector(wuliu) forControlEvents:UIControlEventTouchUpInside];
        
        [self setupstateButtonLabel:@"确认收货" buttonImage:@"extendBtnImage" addbutton:_leftBtn];
        [self.leftBtn addTarget:self action:@selector(shouhuo) forControlEvents:UIControlEventTouchUpInside];
    }else if([model.status isEqualToString:@"500"]){
        self.rightButton.hidden = YES;
        if ([model.isshare intValue] == 0) {
            //可以去晒单
            [self setupstateButtonLabel:@"马上晒单" buttonImage:@"expressBtnImage" addbutton:_leftBtn];
            [self.leftBtn addTarget:self action:@selector(shaidan) forControlEvents:UIControlEventTouchUpInside];
        }else{
        //已经晒过单
            [self setupstateButtonLabel:@"删除" buttonImage:@"deleBtnImage" addbutton:_leftBtn];
            [self.leftBtn addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}


//- (void)orderSuccess
//{
//    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
//    // 当网络状态改变了，就会调用
//    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        switch (status) {
//            case AFNetworkReachabilityStatusUnknown:// 未知网络
//            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
//            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
//                [MBProgressHUD showError:@"请在wifi状态下上传"];
//                break;
//                
//            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
//                //if ([self.productModel.over isEqual:@"1"]) {
//                    [self orderSuccess2];
////                }
////                else{
////                    [self uploadAlasset];
////                }
//                
//                break;
//        }
//    }];
//    [mgr startMonitoring];
//}







- (void)setUpShareCell:(shareModel *)model
{
    
    self.Name.text = [NSString stringWithFormat:@"%@-%@",model.title,model.category];
    self.Standard.text = [NSString stringWithFormat:@"规格:%@   %@页",model.page_standard,model.pagetotal];
    self.Time.text = model.create_time;
    if ([model.picture isKindOfClass:[NSNull class]]) {
        model.picture = nil;
    }
    [BPImageTool setUpUIImageView:self.productImage addNsstringUrl:model.picture];
    [self setupstateButtonLabel:@"删除" buttonImage:@"deleBtnImage" addbutton:_leftBtn];
    [self.leftBtn addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
    if([model.isshare isEqualToString:@"0"]){
        [self setupstateButtonLabel:@"我要分享" buttonImage:@"shareBtnImage" addbutton:_rightButton];
        [self.rightButton addTarget:self action:@selector(fenxiang) forControlEvents:UIControlEventTouchUpInside];
    }else if([model.isshare isEqualToString:@"1"]){
        [self setupstateButtonLabel:@"再次分享" buttonImage:@"shareBtnImage" addbutton:_rightButton];
        [self.rightButton addTarget:self action:@selector(fenxiang2) forControlEvents:UIControlEventTouchUpInside];
    }else{
        BPLog(@"error");
    }
}

- (void)setupstateButtonLabel:(NSString *)labelText buttonImage:(NSString *)imageText addbutton:(UIButton *)button
{
    [button setTitle:labelText forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:imageText] forState:UIControlStateNormal];
}

- (void)jixubianji
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(continueDIY:)]) {
        [self.delegate continueDIY:self];
    }
}

- (void)yinshua
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(goToPrint:)]) {
        [self.delegate goToPrint:self];
    }
}

- (void)xiangqing
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(lookDetail:)]) {
        [self.delegate lookDetail:self];
    }
}

- (void)fenxiang
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(shareProduct:)]) {
        [self.delegate shareProduct:self];
    }
}

- (void)fenxiang2
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(shareAgain:)]) {
        [self.delegate shareAgain:self];
    }
}

- (void)shanchu
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(shanchuCell:)]) {
        [self.delegate shanchuCell:self];
    }
}

- (void)zhifu
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(quicklyPay:)]) {
        [self.delegate quicklyPay:self];
    }
}

- (void)wuliu
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(lookWUliu:)]) {
        [self.delegate lookWUliu:self];
    }
}

- (void)dindan
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(lookDinDan:)]) {
        [self.delegate lookDinDan:self];
    }
}

- (void)shaidan
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(quickShaidan:)]) {
        [self.delegate quickShaidan:self];
    }
}

- (void)shouhuo
{
    if (delegate !=nil && [delegate respondsToSelector:@selector(querenGoods:)]) {
        [self.delegate querenGoods:self];
    }
}





- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
